package com.sqx.modules.tbCoupon.controller.couponUser;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.sys.controller.AbstractController;
import com.sqx.modules.tbCoupon.entity.TbCouponUser;
import com.sqx.modules.tbCoupon.service.TbCouponService;
import com.sqx.modules.tbCoupon.service.TbCouponUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@RestController
@Api(value = "用户端-优惠券", tags = {"用户端-优惠券"})
@RequestMapping(value = "/app/couponUser/")
public class AppTbCouponUserController extends AbstractController {

    @Autowired
    private TbCouponUserService couponUserService;

    @Resource
    private TbCouponService couponService;


    @Login
    @ApiOperation("查看我的优惠券列表")
    @GetMapping(value = "getMyCouponList")
    public Result getMyCouponList(@RequestAttribute Long userId, Integer page, Integer limit, TbCouponUser couponUser) {
        couponUser.setUserId(userId);
        return Result.success().put("data", couponUserService.getMyCouponList(page, limit, couponUser));
    }

    /*
    * 根据优惠卷Id查询优惠卷
    * */
    @Login
    @ApiOperation("根据优惠卷Id查询优惠卷")
    @GetMapping("getCouponById")
    public Result getCouponById(Long couponId) {
        return Result.success().put("data", couponService.getById(couponId));
    }

    @Login
    @ApiOperation("领取新人优惠券")
    @GetMapping("receiveEnvelope")
    public Result receiveEnvelope(@RequestAttribute("userId") Long userId, Long couponId, Integer num) {

        return couponUserService.receiveEnvelope(userId, couponId, num);
    }


    @Login
    @ApiOperation("领取活动优惠券")
    @GetMapping("receiveActivity")
    public Result receiveActivity(@RequestAttribute("userId") Long userId, Long couponId) {
        return couponUserService.receiveActivity(userId, couponId);
    }

    @Login
    @ApiOperation("获取我的优惠券数量")
    @GetMapping("getMyCouponCount")
    public Result getMyCouponCount(@RequestAttribute("userId") Long userId) {
        return Result.success().put("data", couponUserService.getMyCouponCount(userId));
    }


    /**
     * 检测使优惠券过期
     */
    @Scheduled(cron = "0/2 * * * * ?", zone = "Asia/Shanghai")
    public void couponOverdue() {
        List<TbCouponUser> couponUserList = couponUserService.list(new QueryWrapper<TbCouponUser>().eq("status", 0).isNotNull("expiration_time"));
        for (TbCouponUser tbCouponUser : couponUserList) {
            if (tbCouponUser.getExpirationTime().before(new Date())) {
                tbCouponUser.setStatus(2);
                couponUserService.updateById(tbCouponUser);
            }
        }

    }
}